﻿using System;
using System.Collections.Generic;
using System.Text;
using WindowsLive.Writer.Api;
using System.Windows.Forms;

namespace Prabir.wlw.Syntaxhiglighter
{
    [WriterPlugin("f32c3428-b7e9-4f15-a8ea-c502c7ff2e88", "Syntax higlighter",
        Description = "Intert source codes", HasEditableOptions = true,
        PublisherUrl = "http://www.prabir.me", ImagePath = "code.ico")]
    [InsertableContentSource("Synatx higlighter")]
    public partial class Syntaxhiglighter : SmartContentSource
    {
        //WebBrowser browser;
        IWin32Window _dialogOwner;

        public override SmartContentEditor CreateEditor(ISmartContentEditorSite editorSite)
        {
            PropertyPanelEditor editor = new PropertyPanelEditor() { DialogOwner = _dialogOwner, PluginOptions = Options };
            return editor;
        }

        public override string GeneratePublishHtml(ISmartContent content, IPublishingContext publishingContext)
        {
            return GetPublishHtml(content);
        }

        private string GetPublishHtml(ISmartContent content)
        {
            if (content == null)
                return "";

            string code = content.Properties.GetString("Code", "").Replace("&", "&amp;").
                Replace("<", "&lt;").Replace(">", "&gt;");
            string html = string.Format("<pre class=\"brush: {0};{1}\">{2}</pre>", content.Properties.GetString("Lang", "text"), GetOptions(content), code);

            return html;
        }

        private string GetOptions(ISmartContent content)
        {
            StringBuilder sb = new StringBuilder();

            if (content.Properties.GetBoolean("collapse", false))
                sb.Append("collapse:true;");
            if (!content.Properties.GetBoolean("gutter", true))
                sb.Append("gutter:false;");
            if (!content.Properties.GetBoolean("auto-links", true))
                sb.Append("auto-links:false;");
            if (!content.Properties.GetBoolean("toolbar", true))
                sb.Append("toolbar:false;");
            if (!content.Properties.GetBoolean("wrap-lines", true))
                sb.Append("wrap-lines:false;");

            int firstLine = Convert.ToInt32(content.Properties.GetInt("first-line", 1));
            if (firstLine != 1)
            {
                sb.Append("first-line:");
                sb.Append(firstLine);
                sb.Append(";");
            }
            return sb.ToString();
        }

        public override DialogResult CreateContent(IWin32Window dialogOwner, ISmartContent newContent)
        {
            _dialogOwner = dialogOwner;

            string code = "";

            EditForm frm = new EditForm(Options.GetString("Lang", "text"), code);
            DialogResult result = frm.ShowDialog(dialogOwner);
            if (result == DialogResult.OK && !string.IsNullOrEmpty(frm.Code.Trim()))
            {
                newContent.Properties.SetString("Lang", frm.Langauge);
                newContent.Properties.SetString("Code", frm.Code);

                // Other options goes out here.
            }
            else
                result = DialogResult.Cancel;

            frm.Dispose();
            return result;
        }

    }
}
